package com.scu.baseline.controller;

import io.swagger.annotations.*;
import jdk.internal.dynalink.linker.LinkerServices;
import org.apache.ibatis.annotations.Param;
import com.scu.baseline.pojo.Host;
import com.scu.baseline.pojo.HostSimple;
import com.scu.baseline.service.impl.HostServiceImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Host)表控制层
 *
 * @author AlonBurukeses
 * @since 2022-06-23 09:14:21
 */
@RestController
@Api(value = "设备数据接口",tags = "设备数据接口API")
@ApiResponses({
        @ApiResponse(code=400,message="请求参数不完整或者错误"),
        @ApiResponse(code=404,message="找不到页面"),
        @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get"),
        @ApiResponse(code=406,message="页面地址和接口名冲突"),
        @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code=500,message="后台服务逻辑错误")
})
@RequestMapping("host")
public class HostController {
    /**
     * 服务对象
     */
    @Resource
    private HostServiceImpl hostService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "设备详情查询接口",notes = "设备详情查询接口",httpMethod = "GET")
    @ApiImplicitParams(@ApiImplicitParam(name = "id",value = "id",paramType = "query",dataType = "int",required = true))
    @RequestMapping(value = "/findhostbyid",method = RequestMethod.GET)
    public Map<String,Object> findHostById(@RequestParam(value = "id") Integer id) {
        Map<String,Object> map = new HashMap();
        map.put("code",200);
        map.put("msg","修改成功");
        map.put("res",this.hostService.queryById(id));
        return map;
    }
    @ApiOperation(value = "设备概要查询接口",notes = "设备概要查询接口",httpMethod = "GET")
    @RequestMapping(value = "/findhostlist",method = RequestMethod.GET)
    public List<HostSimple> findHostList(){

        return this.hostService.selectSimple();
    }

    /**
     * 新增数据
     *
     * @param host 实体
     * @return 新增结果
     */
    @ApiOperation(value = "设备新增接口",notes = "设备新增接口",httpMethod = "POST")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Map<String,Object> createHost(@RequestBody Map<String,Object> host) {
        String hostMac =  (String)host.get("hostMac");
        HostSimple hostSimple = this.hostService.selectByHostMac(hostMac);
        Map<String,Object> map = new HashMap();
        if(hostSimple==null) {
            this.hostService.insertMap(host);
            hostSimple = this.hostService.selectByHostMac(hostMac);
        }
        map.put("code",200);
        map.put("msg","Suceess");
        map.put("hostId",hostSimple.getHostId());
        return map;
    }

    /**
     * 编辑数据
     *
     * @param host 实体
     * @return 编辑结果
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Map<String,Object> edit(@RequestBody Host host) {
        Map<String,Object> map = new HashMap();
        this.hostService.update(host);
        map.put("code",200);
        map.put("msg","修改成功");
        return map;
    }
    @ApiOperation(value = "设备修改接口",notes = "设备修改接口",httpMethod = "POST")
    @RequestMapping(value = "/editbymap",method = RequestMethod.POST)
    public Map<String,Object> editByMap(@RequestBody Map host) {
        Map<String,Object> map = new HashMap();
        this.hostService.updateByMap(host);
        map.put("code",200);
        map.put("msg","修改成功");
        return map;
    }
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping(value = "/deletebyid",method = RequestMethod.GET)
    public Map<String,Object> deleteById(@RequestParam(value = "id")Integer id) {
        Map<String,Object> map = new HashMap();
        map.put("code",200);
        this.hostService.deleteById(id);
        map.put("msg","删除成功");
        return map;
    }
    @RequestMapping(value = "/selectbyhostname",method = RequestMethod.GET)
    public Map<String,Object> selectByHostName(@RequestParam(value = "hostName")String hostName, @RequestParam(value="page") Integer page){
        Map<String,Object> map = new HashMap();
        map.put("code",200);
        map.put("msg","返回成功");
        map.put("res",this.hostService.selectHostSimpleByHostName(hostName,page));
        return map;
    }
    @RequestMapping(value = "/selectpaged",method = RequestMethod.GET)
    public Map<String,Object> selectPaged(@RequestParam(value="page") Integer page){
        Map<String,Object> map = new HashMap();
        map.put("code",200);
        map.put("msg","返回成功");
        map.put("res",this.hostService.selectHostSimplePaged(page));
        return map;
    }
    @RequestMapping(value = "/selectsimplebyid",method = RequestMethod.GET)
    public Map<String,Object> selectsimplebyid(@RequestParam(value="id") Integer id){
        Map<String,Object> map = new HashMap();
        map.put("code",200);
        map.put("msg","返回成功");
        map.put("res",this.hostService.selectSimpleByHostId(id));
        return map;
    }

    @RequestMapping(value = "/selectsimplebyMac",method = RequestMethod.GET)
    public Map<String,Object> selectsimplebyMac(@RequestParam(value="hostMac") String hostMac){
        Map<String,Object> map = new HashMap();
        map.put("code",200);
        map.put("msg","返回成功");
        map.put("res",this.hostService.selectSimpleByHostMac(hostMac));
        return map;
    }
}

